package com.code.bolg.task;


import com.code.bolg.config.SysConfig;
import com.code.bolg.enums.ResultEnum;
import com.code.bolg.utils.GifDecoder;
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

@Slf4j
@Component
public class BaseTask {
    @Autowired
    private SysConfig sysConfig;




    private int width = 200;
    private int height = 200;


//    @Scheduled(fixedRate = 100000)
    public void reportCurrentTime() {
        log.info("定时压缩任务start");
        long start = System.currentTimeMillis();
        try {
            this.minPath(sysConfig.getBreviaryPath(), sysConfig.getArtworkPath());
        } catch (IOException e) {
            e.printStackTrace();
        }
        log.info("定时压缩任务end,耗时：{}", System.currentTimeMillis() - start);
    }

    /**
     * @param path    原图
     * @param minPath 缩略图
     * @throws IOException
     */
    public void minPath(String path, String minPath) throws IOException {
        File minfile = new File(minPath);
        File file = new File(path);
        if (!minfile.exists()) {
            //创建文件夹
            minfile.mkdir();
        }
        if (!file.exists()) {
            //创建文件夹
            file.mkdir();
        }
        File[] tempList = file.listFiles();
        int length = tempList.length;
        if (length == 0) {
            log.info("原图数量为0没有图片需要压缩");
            return;
        }
        int length1 = minfile.listFiles().length;
        if (length == length1) {
            log.info("压缩图片数量正确,不需要进行压缩操作");
            return;
        }
        for (int index = 0; index < tempList.length; index++) {
            File filel = tempList[index];
            String suffix = filel.getName().substring(filel.getName().lastIndexOf("."));//获取文件后缀名
            //删除不合法文件
            if(!sysConfig.getImgtypes().contains(suffix)){
                filel.delete();
            }
            log.info("types.contains(suffix):{}",sysConfig.getImgtypes().contains(suffix));
            //判断图片，并确定文件后缀
            if (filel.isFile()&&sysConfig.getImgtypes().contains(suffix)) {
                if (ResultEnum.JPG.getMessage().equals(suffix)) {
                    BufferedImage originalImage = ImageIO.read(new FileInputStream(filel));
                    Thumbnails.of(originalImage)
                            .size(width, height)
                            .toFile(minPath + filel.getName());
                } else if (ResultEnum.GIF.getMessage().equals(suffix)) {
                    FileInputStream data = new FileInputStream(filel);
                    GifDecoder.GifImage gif = GifDecoder.read(data);
                    Thumbnails.of(data)
                            .size(width, height)
                            .toFile(minPath + filel.getName());
                }
            }
        }
    }

}
